package cn.ht.jywrwater.module.ht.service.waterqualitydevicefactor;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.ht.jywrwater.module.ht.controller.admin.waterqualitydevicefactor.vo.*;
import cn.ht.jywrwater.module.ht.dal.dataobject.waterqualitydevicefactor.WaterQualityDeviceFactorDO;
import cn.ht.jywrwater.framework.common.pojo.PageResult;

import cn.ht.jywrwater.module.ht.convert.waterqualitydevicefactor.WaterQualityDeviceFactorConvert;
import cn.ht.jywrwater.module.ht.dal.mysql.waterqualitydevicefactor.WaterQualityDeviceFactorMapper;

import static cn.ht.jywrwater.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.ht.jywrwater.module.ht.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 水质设备因子 Service 实现类
 *
 * @author hzx
 */
@Service
@Validated
public class WaterQualityDeviceFactorServiceImpl implements WaterQualityDeviceFactorService {

    @Resource
    private WaterQualityDeviceFactorMapper waterQualityDeviceFactorMapper;

    @Override
    public Long createWaterQualityDeviceFactor(WaterQualityDeviceFactorCreateReqVO createReqVO) {
        // 插入
        WaterQualityDeviceFactorDO waterQualityDeviceFactor = WaterQualityDeviceFactorConvert.INSTANCE.convert(createReqVO);
        waterQualityDeviceFactorMapper.insert(waterQualityDeviceFactor);
        // 返回
        return waterQualityDeviceFactor.getId();
    }

    @Override
    public void updateWaterQualityDeviceFactor(WaterQualityDeviceFactorUpdateReqVO updateReqVO) {
        // 校验存在
        validateWaterQualityDeviceFactorExists(updateReqVO.getId());
        // 更新
        WaterQualityDeviceFactorDO updateObj = WaterQualityDeviceFactorConvert.INSTANCE.convert(updateReqVO);
        waterQualityDeviceFactorMapper.updateById(updateObj);
    }

    @Override
    public void deleteWaterQualityDeviceFactor(Long id) {
        // 校验存在
        validateWaterQualityDeviceFactorExists(id);
        // 删除
        waterQualityDeviceFactorMapper.deleteById(id);
    }

    private void validateWaterQualityDeviceFactorExists(Long id) {
        if (waterQualityDeviceFactorMapper.selectById(id) == null) {
            throw exception(WATER_QUALITY_DEVICE_FACTOR_NOT_EXISTS);
        }
    }

    @Override
    public WaterQualityDeviceFactorDO getWaterQualityDeviceFactor(Long id) {
        return waterQualityDeviceFactorMapper.selectById(id);
    }

    @Override
    public List<WaterQualityDeviceFactorDO> getWaterQualityDeviceFactorList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return waterQualityDeviceFactorMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<WaterQualityDeviceFactorDO> getWaterQualityDeviceFactorPage(WaterQualityDeviceFactorPageReqVO pageReqVO) {
        return waterQualityDeviceFactorMapper.selectPage(pageReqVO);
    }

    @Override
    public List<WaterQualityDeviceFactorDO> getWaterQualityDeviceFactorList(WaterQualityDeviceFactorExportReqVO exportReqVO) {
        return waterQualityDeviceFactorMapper.selectList(exportReqVO);
    }

}
